#coding:utf8

'''html源码抓取工具库'''
import time, re

# 虚拟浏览器库
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.wait import WebDriverWait

# 模拟用户代理
import user_agent as ua

def create_webdriver(load_image=False, timeout=60, default_size=True, show_window=False):
	'''
		创建driver对象
		# 尽量避免重复启动phantomjs
	'''

	'''use chrome handless'''
	chrome_options = webdriver.ChromeOptions()
	if not show_window:
		chrome_options.add_argument('--headless')
		chrome_options.add_argument('--disable-gpu')
	if not load_image:
		chrome_options.add_argument('–-disable-images')
	browser = webdriver.Chrome(chrome_options=chrome_options, executable_path='/Users/haok2/apps/text2voice/driver/chromedriver')

	# 返回Webdriver对象
	browser.set_page_load_timeout(timeout)	# 设置显式超时时间 单位:秒, 默认2分钟
	if default_size is True:
		browser.set_window_size(1440, 900)	# 虚拟浏览器屏幕大小
	elif isinstance(set_size, tuple):
		browser.set_window_size(set_size[0], set_size[1])  # 虚拟浏览器屏幕大小
	else:
		pass
	return browser

'''
	抓取页面源码
	type:id/class
	block_name:网页区域名称,id或者class; 不能有空格, 但可以选用多个class中一个
'''
def get_html(url, driver=create_webdriver(), type=None, block_name=None, wait_time=60):
	try:
		driver.get(url)
		if type == 'id':
			element = WebDriverWait(driver, wait_time).until(
				lambda x: x.find_element_by_id(block_name))	## 根据id截取局部页面
		elif type == 'class':
			element = WebDriverWait(driver, wait_time).until(
				lambda x: x.find_element_by_class_name(block_name))	## 根据class截取局部页面
		elif type =='xpath':
			element = WebDriverWait(driver, wait_time).until(
				lambda x: x.find_elements_by_xpath(block_name))	## 根据元素路径path截取局部页面
			return ('').join([e.get_attribute('outerHTML').encode('utf-8') for e in element])
		else:
			return driver.page_source.encode('utf-8')	## 返回全部页面
		return element.get_attribute('outerHTML').encode('utf-8')	## 字符集转变.decode('utf8', 'ignore').encode('gbk', 'ignore')
	except NoSuchElementException as unfound_error:
		# 未找到元素
		print 'no element '+str(unfound_error)
		return None
	except Exception, e:
		print 'other error '+str(e)
		return None